﻿using System;
using biz.ritter.javapi.lang;
using biz.ritter.javapi.lang.reflect;
using biz.ritter.javapi.util;
using Nofs.Net.Common.Interfaces.Domain;
using java = biz.ritter.javapi.lang;

namespace Nofs.Net.Domain.Impl
{
    public class MethodInvocation : IMethodInvocation
    {
        private List<IMethodParameter> _parameters;
        private String _pwd;
        private String _path;
        private String _method;

        public MethodInvocation(String method, String pwd, String path, List<IMethodParameter> parameters)
        {
            _method = method;
            _pwd = pwd;
            _path = path;
            _parameters = parameters;
        }

        public MethodInvocation(String xml) //throws Exception 
        {
            int firstQuote = xml.indexOf('\"', 0);
            if (firstQuote == (-1))
            {
                throw new System.Exception("invalid method invocation format");
            }
            int secondQuote = xml.indexOf('\"', firstQuote + 1);
            if (secondQuote == (-1))
            {
                throw new System.Exception("invalid method invocation format");
            }
            int thirdQuote = xml.indexOf('\"', secondQuote + 1);
            if (thirdQuote == (-1))
            {
                throw new System.Exception("invalid method invocation format");
            }
            int fourthQuote = xml.indexOf('\"', thirdQuote + 1);
            if (fourthQuote == (-1))
            {
                throw new System.Exception("invalid method invocation format");
            }
            int fifthQuote = xml.indexOf('\"', fourthQuote + 1);
            if (fifthQuote == (-1))
            {
                throw new System.Exception("invalid method invocation format");
            }
            int sixthQuote = xml.indexOf('\"', fifthQuote + 1);
            if (sixthQuote == (-1))
            {
                throw new System.Exception("invalid method invocation format");
            }
            _pwd = xml.substring(firstQuote + 1, secondQuote);
            _path = xml.substring(thirdQuote + 1, fourthQuote);
            _method = xml.substring(fifthQuote + 1, sixthQuote);
            _parameters = new ArrayList<IMethodParameter>();
            for (int i = xml.indexOf("<Parameter", 0); i > 0 && i < xml.length(); i = xml.indexOf("<Parameter", i + 1))
            {
                int end = xml.indexOf("/>", i);
                String parameterXML = xml.substring(i, end + 2);
                _parameters.add(new MethodParameter(parameterXML));
            }
        }

        public MethodInvocation(String path, Method method)
        {
            _parameters = new LinkedList<IMethodParameter>();
            _path = path;
            _method = method.getName();
            int index = (0);

            foreach (Class c in method.getParameterTypes())
            {
                Type type = c.GetType();
                _parameters.add(new MethodParameter(type, index));
            }
        }

        //@Override
        public List<IMethodParameter> GetParameters()
        {
            return Collections.unmodifiableList(_parameters);
        }

        //@Override
        public String GetPwd()
        {
            return _pwd;
        }

        //@Override
        public String GetPath()
        {
            return _path;
        }

        //@Override
        public String MethodName()
        {
            return _method;
        }

        //@Override
        public String ToXML()
        {
            StringBuffer buffer = new StringBuffer();
            buffer.append("<MethodInvocation Pwd=\"");
            buffer.append(_pwd == null ? "" : _pwd);
            buffer.append("\" Path=\"");
            buffer.append(_path);
            buffer.append("\" Name=\"");
            buffer.append(_method);
            buffer.append("\">");
            
            for(int i=0;i<_parameters.size();++i)
            {
                IMethodParameter parameter = _parameters.get(i);
                buffer.append(parameter.ToXML(i));
            }
            buffer.append("</MethodInvocation>");
            return buffer.toString();
        }

    }
}
